//
//  HCGrouthViewController.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/2/4.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCGrouthViewController.h"

#import "ACSelectMediaView.h"

#import "HCGrouthLinesView.h"

#import "WayImageFormUpload.h"

@interface HCGrouthViewController () <HCGrouthLinesViewDelegate,ACSelectMediaViewDismissDelegate> {
    NSMutableArray *images;
    HCPopLoadingView *popLoading;
    NSString *subText;
    HCGrouthLinesView *glView;
}

@end

@implementation HCGrouthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.textTitle = @"成长相册";
    
    [self setRightButtonText:@"发送"];
    
    self.dataRequest.delegate = self;
    
    CGRect frame = [self initializeTextVeiw];
    
    [self initializePhotoView:frame];
    
    // Do any additional setup after loading the view.
}

-(CGRect)initializeTextVeiw {
    glView = [[HCGrouthLinesView alloc] initWithFrame:CGRectMake(0, 0, self.centerView.frame.size.width, YBKAdaptiveH(95))];
    glView.backgroundColor = [UIColor whiteColor];
    glView.delegate = self;
    [self.centerView addSubview:glView];
    return glView.frame;
}

-(void)initializePhotoView:(CGRect)frame {
    //1、得到默认布局高度（唯一获取高度方法）
    CGFloat height = [ACSelectMediaView defaultViewHeight];
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.origin.y+frame.size.height, self.view.bounds.size.width, height)];
    bgView.backgroundColor = [UIColor redColor];
    //2、初始化
    ACSelectMediaView *mediaView = [[ACSelectMediaView alloc] initWithFrame:CGRectMake(0, 0, bgView.frame.size.width, bgView.frame.size.height)];
    //3、选择媒体类型：是否仅选择图片或者其他的等
    mediaView.type = ACMediaTypePhoto;
    //4、随时获取新的布局高度
    [mediaView observeViewHeight:^(CGFloat value) {
        bgView.height = value;
    }];
    mediaView.delegate = self;
    images = [NSMutableArray array];
    
    //5、随时获取已经选择的媒体文件
    [mediaView observeSelectedMediaArray:^(NSArray<ACMediaModel *> *list) {
        for (ACMediaModel *model in list) {
            NSLog(@"%@",model);
            [images addObject:model.image];
        }
    }];
    [bgView addSubview:mediaView];
    [self.centerView addSubview:bgView];
}

-(void)rightBtnAction:(UIButton *)btn {
    NSLog(@"成长相册");
    [self.view endEditing:YES];
    if ([PublicFunction isNull:subText]) {
        [XHToast showBottomWithText:@"发送文字不能为空"];
        return;
    }
    
    
//    hud_hc = [MBProgressHUD showHUDAddedTo:centerView animated:YES];
//    hud_hc.labelText = @"图片上传中,请稍后...";
    popLoading = [[HCPopLoadingView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [popLoading showAnimate];
    
    __block __weak NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    NSMutableArray *arr_dd = [NSMutableArray array];
    
    NSString *image_text_time = [PublicFunction getDatetimestamp111];
    
    for (UIImage *image_d in images) {
        NSData *data_image = UIImageJPEGRepresentation(image_d, 0.5);
        
//        NSLog(@"时间戳:%@",[PublicFunction getDateNow]);
        
        [WayImageFormUpload defaultManmagerWith:data_image andtime:image_text_time AndBlock:^(NSDictionary *str) {
            
            [arr_dd addObject:str];
            
            NSLog(@"存数据:%@",[PublicFunction getDatetimestamp111]);
            
            dic = (NSMutableDictionary *) str;
            if ([dic[@"stat"] integerValue] != 200) {
                NSLog(@"所有图片上传完成");
            }
            
            if (arr_dd.count >= images.count) {
                if ([dic[@"stat"] integerValue] == 200) {
                    [self.dataRequest postHC_Grow_text:HCGETUSER(@"userid") text_record:subText time_record:image_text_time];
                }else{
                    [popLoading hidden];
                    [XHToast showBottomWithText:@"图片上传失败"];
                }

            }
            
        }];
    }

    
    
}

-(void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {
    [popLoading hidden];
    if ([dictionary[@"stat"] integerValue] == 200) {
        [self.navigationController popViewControllerAnimated:YES];
        [XHToast showBottomWithText:@"上传成功"];
    }else{
        
        [XHToast showBottomWithText:@"上传失败"];
    }
}

-(void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {
    [popLoading hidden];
    NSLog(@"网络请求失败--成长相册上传");
}

-(void)hcGrouthAlbum:(NSString *)str {
    subText = [NSString stringWithFormat:@"%@",str];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)action {
    if (glView) {
        [glView endEditing:YES];
    }
    NSLog(@"关闭输入");
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
